const path = require('path')

const resolve = dir => {
  return path.join(__dirname, dir)
}


// 项目部署基础
// 默认情况下，我们假设你的应用将被部署在域的根目录下,
// 例如：https://www.my-app.com/
// 默认：'/'
// 如果您的应用程序部署在子路径中，则需要在这指定子路径
// 例如：https://www.foobar.com/my-app/
// 需要将它改为'/my-app/'
const BASE_URL = process.env.NODE_ENV === 'production'
  ? '/'
  : '/'


const  servrProxy = {
  target: 'http://localhost:9011',
  pathRewrite: { '^/': '' },
  changeOrigin: true
}
const serverProxy = {
  target: 'http://localhost:9011',
  pathRewrite: { '^/': '' },
  changeOrigin: true
}
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const productionGzipExtensions = ['js', 'css']

const webpack = require('webpack')
const isProduction = process.env.NODE_ENV === 'production'

// const UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;



module.exports = {
  lintOnSave: false,
  // Project deployment base
  // By default we assume your app will be deployed at the root of a domain,
  // e.g. https://www.my-app.com/
  // If your app is deployed at a sub-path, you will need to specify that
  // sub-path here. For example, if your app is deployed at
  // https://www.foobar.com/my-app/
  // then change this to '/my-app/'
  baseUrl: BASE_URL,
  // tweak internal webpack configuration.
  // see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
  chainWebpack: config => {
    config.resolve.alias
      .set('@', resolve('src')) // key,value自行定义，比如.set('@@', resolve('src/components'))
      .set('_c', resolve('src/components'))
  },
  // 打包时不生成.map文件
  productionSourceMap: true,
  // 这里写你调用接口的基础路径，来解决跨域，如果设置了代理，那你本地开发环境的axios的baseUrl要写为 '' ，即空字符串
  devServer: {
    host:'0.0.0.0',
    proxy: {
      '^/': serverProxy,
      '/dsmodel': {
        target: 'http://localhost:9011',
        pathRewrite: {'^/': ''},
        changeOrigin: true
      },
    }
  },

// 配置compression-webpack-plugin插件,将前端将文件打包成.gz文件.
//有个js有3M多,太大了,这样打包会小一些
 configureWebpack:{
     resolve:{
       extensions: ['.js', '.vue', '.json',".css"],
       alias:{
         // '@':path.resolve(__dirname, './src'),
         // '@i':path.resolve(__dirname, './src/assets'),
         'vue$': 'vue/dist/vue.esm.js',
         '@': resolve('src'),
       }
     },
     plugins: [
       // Ignore all locale files of moment.js
       new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),

       // 配置compression-webpack-plugin压缩
       new CompressionWebpackPlugin({
         algorithm: 'gzip',
         test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
         threshold: 10240,
         minRatio: 0.8
       }),
       new webpack.optimize.LimitChunkCountPlugin({
         maxChunks: 5,
         minChunkSize: 100
       })
     ],


   }
}
